package com.qf.personal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import com.qf.common.db.utils.PageCommonBeanUtils;
import com.qf.personal.entiy.Sign;
import com.qf.personal.entiy.Vo.EmpVo;
import com.qf.personal.mapper.SignMapper;
import com.qf.personal.service.SignService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class SignServiceImpl implements SignService {
    @Resource
    private SignMapper signMapper;

    @Override
    @Transactional
    public RespResult<String> add(int id) {
        Date date = new Date();
        EmpVo e = signMapper.selectById(id);
        try {
            if (!ObjectUtils.isEmpty(e)) {
                signMapper.add(id, date);
                return RespResult.success("新增成功");
            } else {
                throw new ServiceException(ResultCode.SYS_ERROR);
            }
        } catch (ServiceException se) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    @Override
    public IPage<Sign> list(int page, int size, int id) {
        QueryWrapper<Sign> qw = new QueryWrapper<>();
        qw.select("creatTime").eq("empId", id);
        return PageCommonBeanUtils.copyPage(signMapper.selectPage(new Page<>(page, size), qw), new Page<>(), Sign::new);
    }
}
